/**
*电源柜js
*/ 

var	powerBoxDS;
var powerBoxFormPanel;
function doPowerBoxDelete(id)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该信息?', 
	function(btn) {
		if(btn == 'yes') // 选中了是按钮	
		{
			JPowerBoxWeb.deletePowerBox(id,function(data)
			{
				if(data)
				{ 
					Ext.Msg.alert('成功',"操作成功！"); 
					powerBoxDS.load({params:{start:0,limit:17,'<%=Constant.BTS_CODE%>':'<%=strbtscode%>'}});	 
					powerBoxFormPanel.getForm().reset();			
				}else
				{
					Ext.Msg.alert("失败","操作失败!");
				}
			})
		}
	})	
}


Ext.onReady(function(){  
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

	var powerBoxType = Ext.data.Record.create([								
							   {name: 'id', type: "string"},
							   {name: 'btscode', type: "string"},
							   {name: 'btsname', type: "string"},
							   {name: 'type', type: "string"},
							   {name: 'manufacturer', type: "string"},
							   {name: 'num', type: "string"},
							   {name: 'dcvoltage',  type: 'string'},
							   {name: 'opentime', type: "date"},
							   {name: 'commtype', type: "string"},
							   {name: 'opentime', type: 'date'},
							   {name: 'monmodel', type: 'string'},
							   {name: 'monnum',  type: 'string'},
							   {name: 'maintenance',  type: 'string'},
							   {name: 'refreshtime',  type: 'date'},
							   {name: 'memo',  type: 'string'},
							   {name: 'pboxOutI',  type: 'string'},
							   {name: 'pboxNum',  type: 'string'},
							   {name: 'pboxWorkSta',  type: 'string'}
						 ]); 
	var powerBoxCM = new Ext.grid.ColumnModel(
			[
				new Ext.grid.CheckboxSelectionModel({singleSelect: true}), 	            
				{header: "流水号", sortable: true, dataIndex: 'id',align:'left'},
				{header: "基站编号", sortable: true, dataIndex: 'btscode',align:'left'},
				{header: "基站名称",  sortable: true, dataIndex: 'btsname',align:'left'},	
				{header: "规格型号",  sortable: true, dataIndex: 'type',align:'left'},	
				{header: "数量",  sortable: true, dataIndex: 'num',align:'left'},	
				{header: "厂家",  sortable: true, dataIndex: 'manufacturer',align:'left'}, 
				{header: "输出直流电压",  sortable: true, dataIndex: 'dcvoltage',align:'left'}				
			]
		);				 
	
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doPowerBoxDelete(\"" + id  +   "\")'>";  
	}
	
	
	
	powerBoxDS = new Ext.data.Store({
				proxy: new Ext.data.DWRProxy(JPowerBoxWeb.getPowerBoxList, true),	
				reader: new Ext.data.ListRangeReader({
					totalProperty: 'totalSize',
					id: 'id'
				}, powerBoxType),
				sortInfo:{field:'id', direction:'ASC'}// 排序信息
			});

	
	var towerMountedGridDiv = Ext.get("mainTabPanelDIV");
	var powerBoxGrid = new Ext.grid.GridPanel({			 	
			ds: powerBoxDS,
			cm: powerBoxCM, 
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) {
                            powerBoxFormPanel.getForm().loadRecord(rec);
                        }
                    }
                }),
			autoWidth : true,  
			autoScroll : true,  
			autoHeight　: false,
			region : 'center',
			height: 400,
			minSize: 200,		
			frame:true,
			collapsible:false, 
			enableDragDrop:false, 		
			bbar: new Ext.PagingToolbar({
					pageSize: 17,
					store: powerBoxDS,
					displayInfo: true,
					displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
					emptyMsg: "没有记录"
				})
		});

	powerBoxDS.on('load', function() {
		powerBoxGrid.getSelectionModel().selectFirstRow(); 
	});		

	// Setup the form panel
	powerBoxFormPanel = new Ext.form.FormPanel({
		region     : 'south', 
		bodyStyle  : 'padding: 10px; background-color: #DFE8F6',  
		height: 250,
		minSize: 250,
		maxSize: 400,
		frame:true,
		buttonAlign:'right',
		layout:'column', 
		items:[
			{
				layout:'column', 
				items:[
					 {
						columnWidth:.33,
						layout: 'form',  
						labelAlign:'right',	
						labelWidth:70,	 
						items: [
							{
								xtype:'hidden',
								fieldLabel: '流水号',
								width:150,
								disabled:true,
								name: 'id'
							}, {
								xtype:'textfield',
								readOnly: true,
								fieldLabel: '基站编号', 
								width:150,
								disabled:true,
								name: 'btscode'
							}, {
								xtype:'textfield',
								fieldLabel: '基站名称',
								width:150,
								readOnly: true,
								disabled:true,
								name: 'btsname'
							}, {
								xtype:'numberfield',
								fieldLabel: '输出直流电压',
								width:150,
								name: 'dcvoltage',
								decimalPrecision:6,
								disabled:true,
								allowBlank:false,
								blankText :'请输入输出直流电压'	
							}]
					 },
					 {
						columnWidth:.33,
						layout: 'form',  
						labelAlign:'right',	
						labelWidth:70,	 
						items: [
							{
								xtype:'numberfield',
								fieldLabel: '数量',
								width:150,
								value:-1,
								disabled:true,
								name: 'num'
							},
							{
								xtype:'textfield',
								fieldLabel: '规格型号',
								width:150,
								disabled:true,
								name: 'type'
							}, {
								xtype:'textfield',
								fieldLabel: '厂家',
								width:150,
								name: 'manufacturer'
							}]
					 },
					 {
						columnWidth:.34,
						layout: 'form', 
						labelAlign:'right',							
						labelWidth:70,	 
						items: [
							{
								xtype:'datefield',
								fieldLabel: '启用日期',
								format : "Y-m-d",
								width:150,
								disabled:true,
								allowBlank:false,
								name: 'opentime'
							}, {
								xtype:'textfield',
								fieldLabel: '输出电流',
								width:150,
								disabled:true,
								name: 'pboxOutI'
							}]
					 }
				]
			},
			{
				xtype:'tabpanel',
				plain:true,
				activeTab: 0,
				height:95, 
				deferredRender: false,
				defaults:{bodyStyle:'padding:10px'},
				items:[
					{
						title:'整流模块数据',
						frame:true,
						layout:'column', 
						items:[
							 {
								columnWidth:.25,
								layout: 'form',  
								labelAlign:'right',	
								labelWidth:70,	 
								items: [
									{
										xtype:'textfield', 
										fieldLabel: '型号', 
										width:100,
										disabled:true,
										name: 'commtype'
									}]
							 },
							 {
								columnWidth:.25,
								layout: 'form', 
								labelAlign:'right',									
								labelWidth:70,	 
								items: [
									{
										xtype:'numberfield',
										fieldLabel: '容量',
										width:100,
										disabled:true,
										name: 'commcapacity'
									}]
							 },
							 {
								columnWidth:.25,
								layout: 'form',  
								labelAlign:'right',	
								labelWidth:70,	 
								items: [
									{
										xtype:'numberfield',
										fieldLabel: '数量',
										width:100,
										value:-1,
										disabled:true,
										name: 'monnum'
									}]
							 },
							 {
								columnWidth:.25,
								layout: 'form', 
								labelAlign:'right',			
								labelWidth:70,	 
								items: [
									{
										xtype:'textfield',
										fieldLabel: '工作状态',
										width:100,
										disabled:true,
										name: 'pboxWorkSta'
									}]
							 }
						]
					},{
						title:'监控模块数据',
						frame:true,
						layout:'column', 
						labelAlign :'center',
						items:[
							 {
								columnWidth:.25,
								layout: 'form',  
								labelWidth:70,	 
								labelAlign:'right',	
								items: [
									{
										xtype:'textfield', 
										fieldLabel: '型号', 
										width:100,
										disabled:true,
										name: 'monmodel'
									}]
							 },
							 {
								columnWidth:.25,
								layout: 'form',  
								labelWidth:70,	
								labelAlign:'right',									
								items: [
									{
										xtype:'textfield',
										fieldLabel: '数量',
										width:100,
										disabled:true,
										name: 'pboxNum'
									}]
							 },
							 {
								columnWidth:.25,
								layout: 'form',  
								labelWidth:70,	
								labelAlign:'right',			
								items: [
									{
										xtype:'textfield',
										fieldLabel: '更换及维修记录',
										width:100,
										disabled:true,
										name: 'maintenance'
									}]
							 },
							 {
								columnWidth:.25,
								layout: 'form',  
								labelAlign:'right',	
								labelWidth:70,	 
								items: [
									{
										xtype:'textarea',
										fieldLabel: '备注',
										width:100,
										disabled:true,
										name: 'memo'
									}]
							 }
						]
					}]
			}	 
			] 	
	});



	//Simple 'border layout' panel to house both grids
	
	var displayPanel = new Ext.Panel({
		width    : towerMountedGridDiv.getComputedWidth(),
		height   : 500,
		layout   : 'border',
		renderTo : 'PowerBoxTab',
		items    : [
			powerBoxGrid,
			powerBoxFormPanel
		]
	});
	
	
	function savePowerBox()
	{
		if (!powerBoxFormPanel.getForm().isValid())
			return;	
			
		var form = powerBoxFormPanel.getForm().getValues(false);	
		PowerBoxBO = {};
		PowerBoxBO.id = form.id;
		PowerBoxBO.btscode = form.btscode;
		PowerBoxBO.btsname= form.btsname;
		PowerBoxBO.type = form.type;
		PowerBoxBO.manufacturer = form.manufacturer;
		PowerBoxBO.num = form.num; 
		PowerBoxBO.dcvoltage = form.dcvoltage;
		
	
		
		var opentimeDate = new Date();	
		var arrPlanTime = form.opentime.split("-");		
				
		var yearOth = arrPlanTime[0];
		var monthOth = arrPlanTime[1];
		var dayOth = arrPlanTime[2];
		opentimeDate.setFullYear(yearOth,monthOth-1,dayOth);
		
		PowerBoxBO.opentime = opentimeDate;
		PowerBoxBO.pboxOutI = form.pboxOutI;
		
		PowerBoxBO.commtype = form.commtype;
		PowerBoxBO.commcapacity = form.commcapacity;
		PowerBoxBO.monnum = form.monnum;
		PowerBoxBO.pboxWorkSta = form.pboxWorkSta;
		
		PowerBoxBO.monmodel = form.monmodel;
		PowerBoxBO.pboxNum = form.pboxNum;
		PowerBoxBO.maintenance = form.maintenance;
		PowerBoxBO.memo = form.memo;
		
		
		
		
			
		JPowerBoxWeb.savePowerBox(PowerBoxBO,function(data)
		{
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				loadDate(powerBoxDS,powerBoxFormPanel); 
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}
		})
			
	}
		
	loadDate(powerBoxDS,powerBoxFormPanel);
	
})